home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / amiga.c < prev    next >
C/C++ Source or Header  |  2000-01-16  |  3KB  |  133 lines

  1. #ifdef stat
  2. #undef stat
  3. #endif
  4. #ifdef fopen
  5. #undef fopen
  6. #endif
  7. #ifdef opendir
  8. #undef opendir
  9. #endif
  10. #ifdef mkdir
  11. #undef mkdir
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/param.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <dirent.h>
  21.  
  22. __BEGIN_DECLS
  23. /**
  24.  * Provide wrappers for the stat, fopen, and opendir functions that massage
  25.  * the file names given to them as arguments so that UNIX "." and ".."
  26.  * path names are translated to their AmigaOS equivalents. This is done so
  27.  * that no intervention is done in terms of file semantics to the jikes
  28.  * source.
  29.  */
  30. static char buf[MAXPATHLEN+1];
  31. static char cwd[MAXPATHLEN+1];
  32. static char pathComponent[MAXPATHLEN+1];
  33.  
  34. static void ix_out(char *s)
  35. {
  36.   int ptr;
  37.  
  38.   if (strcmp(s, ".") == 0) {
  39.     if (buf[0] == '\0') {
  40.       strcat(buf, cwd);
  41.     }else{
  42.       ptr = strlen(buf)-1;
  43.       if (buf[ptr] == '/') {
  44.         buf[ptr] = '\0';
  45.       }
  46.     }
  47.   }else{
  48.     if (strcmp(s, "..") == 0) {
  49.       strcat(buf, "/");
  50.     }else{
  51.       strcat(buf, s);
  52.     }
  53.   }
  54. }
  55.  
  56. static char *
  57. ix_path(const char *path)
  58. {
  59.   int len;
  60.   char sep[2];
  61.   int appendSep, skipNext = 0;
  62.   int i, j;
  63.  
  64.   buf[0] = '\0';
  65.   cwd[0] = '\0';
  66.   pathComponent[0] = '\0';
  67.   sep[1] = '\0';
  68.   getcwd(cwd, sizeof(cwd));
  69.   len = strlen(path);
  70.  
  71.   for (i=0, j=0; i<len; i++) {
  72.     if (path[i] == '/' || path[i] == ':') {
  73.       pathComponent[j] = '\0';
  74.       if (j != 0) {
  75.         ix_out(pathComponent);
  76.         if (buf[0] != '\0' &&
  77.         ((strcmp(pathComponent, ".") == 0 && buf[strlen(buf)-1] == ':') ||
  78.          (strcmp(pathComponent, "..") == 0 && buf[strlen(buf)-1] == '/'))){
  79.       appendSep = 0;
  80.     }else{
  81.       appendSep = 1;
  82.     }
  83.         j = 0;
  84.         pathComponent[0] = '\0';
  85.       }
  86.       sep[0] = path[i];
  87.       if (appendSep && !skipNext) {
  88.         strcat(buf, sep);
  89.       }
  90.       /* Constructs of the type FOO:/bar are *probably* caused by appending
  91.        * UNIX-style a path to a directory, so we skip the bogus "/".
  92.        */
  93.       if (path[i] == ':' && path[i+1] == '/') {
  94.         skipNext = 1;
  95.       }else{
  96.         skipNext = 0;
  97.       }
  98.     }else{
  99.       pathComponent[j++] = path[i];
  100.     }
  101.   }
  102.   if (j > 0) {
  103.     pathComponent[j] = '\0';
  104.     ix_out(pathComponent);
  105.   }
  106.   return buf;
  107. }
  108.  
  109. int
  110. mystat(const char *path, struct stat *sb)
  111. {
  112.   return stat(ix_path(path), sb);
  113. }
  114.  
  115. FILE *
  116. myfopen(char *path, char *mode)
  117. {
  118.   return fopen(ix_path(path), mode);
  119. }
  120.  
  121. DIR
  122. *myopendir(const char *path)
  123. {
  124.   return opendir(ix_path(path));
  125. }
  126.  
  127. int
  128. mymkdir(const char *path, mode_t mode)
  129. {
  130.   return mkdir(ix_path(path), mode);
  131. }
  132. __END_DECLS
  133.